#include "TextureManager.hpp"

namespace sl
{
	namespace tmp
	{
		sf::Texture* TextureManagerS::LoadFromFile(const std::string& filename, const sf::Color& transparent, const std::string& name)
		{
			std::string tmpname = name;

			if (tmpname == "")
				tmpname = GetNameFromFilename(filename);

			if (Get(tmpname) != NULL)
				return Get(tmpname);

			sf::Texture img;

			try
			{
				sf::Image itmp;
				if (!itmp.LoadFromFile(filename))
					return NULL;

				itmp.CreateMaskFromColor(transparent);

				if (!img.LoadFromImage(itmp))
					return NULL;
			}
			catch (std::exception &e)
			{
				::sl::Exception(e.what(), ::sl::ErrorCode::LoadTexture);
				return NULL;
			}

			return Insert(tmpname, img);
		}
	}
}